package wowmobileclient;

import java.io.IOException;

class Fight
{
    FightView fightview;
    FightParser fightParser;
    Client client;
    Canvas canv;
    int target;
    int actionType;
    int atack;
    int iteration;
    int[] startFight;


    public Fight(FightView fv, Client c, Canvas canv, int[] startFight, String startFight2)
    {
        fightview=fv;
        client=c;
        this.canv=canv;
        iteration=0;
        this.startFight=startFight;
        fightParser=new FightParser(startFight2, client.getLogin());
        fillActions();
    }

    private void fillActions()
    {
        target = -1;
        actionType = -1;
        atack = -1;
    }

    void fight()
    {
        boolean isFight=true;
        String answer;
        int keyState;
        try {
                answer = client.getC().getAnswer();
                fightview.fh.drawMsg("   "+answer);
                //System.out.println(answer);
            } catch (IOException ex) {
                ex.printStackTrace();
                return;
            }
        while(isFight)
        {
            while(answer.equals("A"))
            {
                fightview.fh.draw(iteration);
                canv.flushGraphics();
                keyState= canv.getKeyStates();
                if(keyState==0)
                {
                    keyState=canv.getLastKey();
                    canv.setLastKey(0);
                }
                prepareAction(keyState);
                try
                {
                    answer = client.getC().getAnswer();
                }
                catch (IOException ex)
                {
                    ex.printStackTrace();
                }
                try
                    {
                    Thread.sleep(100);
                    }
                catch (InterruptedException ex)
                {
                    ex.printStackTrace();
                }
                
            }
            fightview.draw(startFight[0],startFight[1]);
            canv.flushGraphics();
            try
            {
                    Thread.sleep(100);
            }
            catch (InterruptedException ex)
            {
                    ex.printStackTrace();
            }
            String tmp="";
            try {
                answer = client.getC().getAnswer();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            while(!answer.equals("A"))
            {
                try {
                
                
                
                if(answer.equals("F"))
                    return;
                tmp+=fightParser.parseinfight(answer)+"\n";
                fightview.fh.drawMsg(tmp);
                answer = client.getC().getAnswer();
            } catch (IOException ex) {
                ex.printStackTrace();
                return;
            }
            }
        }
    }


    private void prepareAction(int keyState)
    {
        keyState=changeIntoNr(keyState);
        if(keyState!=-1)
        {
            switch(iteration)
            {
                case 0:
                    if(keyState==0)
                    {
                        actionType=0;
                        sendAction();
                        break;
                    }
                    else if(keyState==1)
                    {
                        actionType=keyState;
                        iteration+=2;
                    }
                    else if(keyState<4)
                    {
                        actionType=keyState;
                        iteration++;
                    }
                    client.getC().sendString("n");
                    break;
                case 1: //brakuje uzaleznienia od tego ile skilli lub przedmiotow ma player
                    atack=keyState;
                    iteration++;
                    client.getC().sendString("n");
                    break;
                case 2:
                    if(keyState==0)
                    {
                        target=7;
                        sendAction();
                        break;
                    }
                    else if(keyState<7)
                    {
                        target=keyState;
                        sendAction();
                        break;
                    }
                    client.getC().sendString("n");
                    break;
            }
        }
        else
            client.getC().sendString("n");
    }

    private void sendAction()
    {
        String tmp="";
        if(actionType==0)
            tmp="0";
        else
        {
            actionType--;
            tmp+=target+","+actionType+","+atack;
        }
        client.getC().sendString(tmp);
        fillActions();
        iteration=0;
    }

    private int changeIntoNr(int keyState)
    {
        switch(keyState)
        {
            case 512:
                return 1;
            case 50:
                return 2;
            case 1024:
                return 3;
            case 52:
                return 4;
            case 53:
                return 5;
            case 54:
                return 6;
            case 2058:
                return 7;
            case 56:
                return 8;
            case 4096:
                return 9;
            case 42:
                return 10;
            case 48:
                return 0;
            case 35:
                return 11;
        }
        return -1;
    }
}
